import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    isLogin: false,
    token: "",
    userInfo: "",
  	// 导航栏高度
  	StatusBar: {
  		statusBar: 0,
  		customBar: 0,
		screenHeight:0,
  	}
  },
  getters: {
    // 是否登录
    hasLogin(state) {
      return !!state.token;
    },
  },
  mutations: {
    login(state, val) {
      state.isLogin = true;
    },
	// 设置导航栏高度
	SET_STATUSBAR(state, value) {
		state.StatusBar = value
	},
    //更新token
    setToken(state, data) {
      const { token, userInfo, uid, userID, userSig } = data;
      state.isLogin = true;
      state.token = token;
      state.userInfo = userInfo;

      // uni.setStorageSync("token", token);
      // uni.setStorageSync("userInfo", userInfo);
      uni.setStorageSync("userID", userID);
      uni.setStorageSync("userSig", userSig);
      this.dispatch("getUserInfo"); //更新用户信息
    },
    //退出登录
    logout(state) {
      state.token = "";
      state.userInfo = "";
      state.isLogin = false;

      setTimeout(() => {
        state.userInfo = {};
      }, 1100);
    },
    //更新state的数据
    setStateAttr(state, param) {
      if (param instanceof Array) {
        for (let item of param) {
          state[item.key] = item.val;
        }
      } else {
        state[param.key] = param.val;
      }
    },
    //更新state obj对象的数据
    setStateObj(state, param) {
      for (let item of param) {
        state[item.obj][item.key] = item.val;
      }
    },
  },
  actions: {
    //更新用户信息
    async getUserInfo({ state, commit }) {},
  },
});

export default store;
